package com.gym.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gym.pojo.Food;
import com.gym.pojo.FoodSort;
import com.gym.service.FoodService;
import com.gym.service.FoodSortService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FoodController {

    @Resource
    FoodService foodService;

    @Resource
    FoodSortService foodSortService;

    @RequestMapping("/toFood")
    @ResponseBody
    public Map<String,Object> toFood(Integer page,Integer limit,String foodName){

        Page<Food> foods = new Page<>(page,limit);
        LambdaQueryWrapper<Food> queryWrapper = new LambdaQueryWrapper<>();
        //根据 食物名 模糊查询
        queryWrapper.like(foodName != null,Food::getFoodName,foodName);
        Page<Food> foodPage = foodService.page(foods,queryWrapper);
        List<Food> foodList = foodPage.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("msg","0");
        map.put("data",foodList);
        map.put("count",foodPage.getTotal());
        return map;
    }

    @RequestMapping("/addFood")
    @ResponseBody
    public Map<String,Object> addFood(Food food) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = foodService.save(food);
        if (save) {
            map.put("save", save);
            map.put("data", "新增成功");
        } else {
            map.put("save", save);
            map.put("data", "新增失败");
        }
        return map;
    }

    @RequestMapping("/delFood")
    @ResponseBody
    public Map<String,Object> delFood(String[] foodId) {
        HashMap<String, Object> map = new HashMap<>();
        //批量删除
        boolean save = foodService.removeByIds(Arrays.asList(foodId));
        if (save){
            map.put("save",save);
            map.put("data","删除成功");
        }else {
            map.put("save",save);
            map.put("data","删除成功");
        }
        return map;
    }

    @RequestMapping("/updateFood")
    @ResponseBody
    public Map<String,Object> updateFood(Food food) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = foodService.updateById(food);
        if (save) {
            map.put("save", save);
            map.put("data", "信息更新成功");
        } else {
            map.put("save", save);
            map.put("data", "信息更新失败");
        }
        return map;
    }


    @RequestMapping("/toFoodSort")
    @ResponseBody
    public Map<String, Object> toFoodSort(Integer page,Integer limit,String foodsortName){

        Page<FoodSort> foodSorts = new Page<>(page,limit);
        LambdaQueryWrapper<FoodSort> queryWrapper = new LambdaQueryWrapper<>();
        //根据 食物名 模糊查询
        queryWrapper.like(foodsortName != null,FoodSort::getFoodsortName,foodsortName);
        Page<FoodSort> foodSortPage = foodSortService.page(foodSorts,queryWrapper);
        List<FoodSort> foodSortList = foodSortPage.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("msg","0");
        map.put("data",foodSortList);
        map.put("count",foodSortPage.getTotal());
        return map;
    }

    @RequestMapping("/addFoodSort")
    @ResponseBody
    public Map<String,Object> addFoodSort(FoodSort foodSort) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = foodSortService.save(foodSort);
        if (save) {
            map.put("save", save);
            map.put("data", "新增成功");
        } else {
            map.put("save", save);
            map.put("data", "新增失败");
        }
        return map;
    }

    @RequestMapping("/delFoodSort")
    @ResponseBody
    public Map<String,Object> delFoodSort(String foodsortId) {
        HashMap<String, Object> map = new HashMap<>();
        //批量删除
        boolean save = foodSortService.removeByIds(Arrays.asList(foodsortId));
        if (save){
            map.put("save",save);
            map.put("data","删除成功");
        }else {
            map.put("save",save);
            map.put("data","删除成功");
        }
        return map;
    }

    @RequestMapping("/updateFoodSort")
    @ResponseBody
    public Map<String,Object> updateFoodSort(FoodSort foodSort) {
        HashMap<String, Object> map = new HashMap<>();
        boolean save = foodSortService.updateById(foodSort);
        if (save) {
            map.put("save", save);
            map.put("data", "信息更新成功");
        } else {
            map.put("save", save);
            map.put("data", "信息更新失败");
        }
        return map;
    }

}
